<?php

class SiteController extends Controller
{
	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionList()
	{
	   $action = Yii::app()->request->getParam('action');
	   $cateid = Yii::app()->request->getParam('cateid');
	   $order  = Yii::app()->request->getParam('order');
	   $brandid  = Yii::app()->request->getParam('brandid');
	   
       if($action == "save"){
         	$ProductName = Yii::app()->request->getParam('ProductName');
            $condition = ' `ProductName` like \'%'.$ProductName.'%\' ';
       } elseif($cateid) {
			$condition = 'cateid='.$cateid;
       } else if($brandid){
       	    $condition = 'BrandID='.$brandid;
       }
       
       #排序
	   if($order){
            $order = 'cateid desc ';
       } else {
			$order = 'Price desc ';
       }
       
		$dataProvider=new CActiveDataProvider('HyBSysProducts',array(
			   'criteria'=>array(
			        'condition'=>$condition,
			        'order'=> $order,
			       // 'with'=>array('author'),
			    ),
			    'pagination'=>array(
			        'pageSize'=>10,
			    ),
			));
		
		
		#首页品牌链接
		$linksData = $this->geBandlis();		
		
		#类别
		$criteria=new CDbCriteria;
		$criteria->select='CateName';  // 只选择 'title' 列
		$criteria->condition='SysProCategoryID=:SysProCategoryID';
		$criteria->params=array(':SysProCategoryID'=>$cateid);
		$cates=HyBSysProcategory::model()->find($criteria); // $params 不需要了
		
		$this->render('list',array(
			'dataProvider'=>$dataProvider,
		    'linksData' => $linksData,
		    'cates' => $cates,
		));
	}
	
	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionView($id)
	{
		#荻取该商品有多少人预订成功
		$SQL = "SELECT count(*) FROM hy_b_order where productID = ".$id;
        $dataReader = Yii::app()->db->createCommand($SQL)->query();
		$orderDate = $dataReader->readAll();
		
		if($orderDate) $orderNum = $orderDate[0];
		$orderNum = 1;
		#首页品牌链接
		$linksData = $this->geBandlis();	
		$model = HyBSysProducts::model()->find('SysProductsID=:SysProductsID ',array( ':SysProductsID'=>$id,));
		
		if($orderNum < $model->Ren2){
			$nowPrice = $model->Price;
		} else if($orderNum < $model->Ren3){
			$nowPrice = $model->Price2;
		} else {
			$nowPrice = $model->Price3;
		}
		
		$weikuang = $nowPrice - $model->DingJin;
		
		$this->render('view',array(
			'model'     => $model,
		    'linksData' => $linksData,
		    'orderNum'  => $orderNum,
		    'nowPrice'  => $nowPrice,
		    'weikuang'  => $weikuang,
		));
	}
	
	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex()
	{
		// renders the view file 'protected/views/site/index.php'
		// using the default layout 'protected/views/layouts/main.php'
		
		
		#荻取该商品有多少人预订成功
		$SQL = "SELECT * FROM hy_b_sys_products where CateID = 11010 limit 6 ";
        $dataReader = Yii::app()->db->createCommand($SQL)->query();
		$prolist1 = $dataReader->readAll();
		
		$SQL = "SELECT * FROM hy_b_sys_products where CateID = 11011 limit 6 ";
        $dataReader = Yii::app()->db->createCommand($SQL)->query();
		$prolist2 = $dataReader->readAll();
		
		
		$SQL = "SELECT * FROM hy_b_sys_products where CateID = 11012 limit 6 ";
        $dataReader = Yii::app()->db->createCommand($SQL)->query();
		$prolist3 = $dataReader->readAll();
		
		
		$SQL = "SELECT * FROM hy_b_sys_products where CateID = 11013 limit 6 ";
        $dataReader = Yii::app()->db->createCommand($SQL)->query();
		$prolist4 = $dataReader->readAll();
		
		
		$SQL = "SELECT * FROM hy_b_sys_products where CateID = 11014 limit 6 ";
        $dataReader = Yii::app()->db->createCommand($SQL)->query();
		$prolist5 = $dataReader->readAll();
		
		
		
		$this->render('index',  array( 
            'linksData'=>$this->geBandlis(),
		    'prolist1'=>$prolist1,		
		    'prolist2'=>$prolist2,		
		    'prolist3'=>$prolist3,		
		    'prolist4'=>$prolist4,		
		    'prolist5'=>$prolist5,		
		));
	}

	/**
	 * This is the action to handle external exceptions.
	 */
	public function actionError()
	{
		if($error=Yii::app()->errorHandler->error)
		{
			if(Yii::app()->request->isAjaxRequest)
				echo $error['message'];
			else
				$this->render('error', $error);
		}
	}

	/**
	 * Displays the contact page
	 */
	public function actionContact()
	{
		$model=new ContactForm;
		if(isset($_POST['ContactForm']))
		{
			$model->attributes=$_POST['ContactForm'];
			if($model->validate())
			{
				$name='=?UTF-8?B?'.base64_encode($model->name).'?=';
				$subject='=?UTF-8?B?'.base64_encode($model->subject).'?=';
				$headers="From: $name <{$model->email}>\r\n".
					"Reply-To: {$model->email}\r\n".
					"MIME-Version: 1.0\r\n".
					"Content-type: text/plain; charset=UTF-8";

				mail(Yii::app()->params['adminEmail'],$subject,$model->body,$headers);
				Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');
				$this->refresh();
			}
		}
		$this->render('contact',array('model'=>$model));
	}

	/**
	 * Displays the login page
	 */
	public function actionLogin()
	{
		$model=new LoginForm;

		// if it is ajax validation request
		if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}

		// collect user input data
		if(isset($_POST['LoginForm']))
		{
			$model->attributes=$_POST['LoginForm'];
			// validate user input and redirect to the previous page if valid
			if($model->validate() && $model->login())
				$this->redirect(Yii::app()->user->returnUrl);
		}
		// display the login form
		$this->render('login',array('model'=>$model));
	}

	/**
	 * Logs out the current user and redirect to homepage.
	 */
	public function actionLogout()
	{
		Yii::app()->user->logout();
		$this->redirect(Yii::app()->homeUrl);
	}
	
	/**
	 * 统计生成订单信息
	 *
	 */
	public function actionPay(){
		$this->isLogin();//验证是否登陆

		$criteria = new CDbCriteria( ) ; 
        $criteria -> condition = ' SysProductsID  = :id' ; 
        $criteria -> params = array (':id' => $_GET['id']) ; 
        $pro = HyBSysProducts:: model()->find($criteria) ; 
		$price =  $pro->Price;
		$nums  = $_GET['num'];
		$totolprice = $nums *  $price;
		$totolprice = number_format($totolprice, 2);
		$this->render('pay',array('pro' => $pro,'num' => $_GET['num'],'totolprice' => $totolprice));
	}


	/**
	 * 将购物支付金存储
	 *
	 */
	public function actionSaveDate(){
		$orderNum = date("YmdHis",time()).rand(10,100);
		$_POST['orderNum'] = $orderNum;
		$_POST['memberID']= Yii::app()->session['user_id'];
		$_POST['Date'] = time();
		Yii::app()->db->createCommand()->insert('hy_b_order', $_POST);
		$this->render('success');
	}
	

	//首页品牌列表
    public function geBandlis() {
		$SQL = "SELECT * FROM hy_b_brandcategory where ParentID = 110";
        $dataReader = Yii::app()->db->createCommand($SQL)->query();
		return $linksData = $dataReader->readAll();
    }
    	

}